//
//  InServerCompangyVC.m
//  u76ho
//
//  Created by m-air-01 on 15/12/28.
//  Copyright © 2015年 LV. All rights reserved.
//

#import "InServerCompangyVC.h"
#import "InServeiCompanyFirCell.h"
#import "AllCompentViewController.h"
#import "CompanySelfCheckVC.h"//企业自检
#import "DynaModel.h"
#import "SendPictureViewController.h"
#import "BoostViewController.h"
@interface InServerCompangyVC ()
<UITableViewDelegate,UITableViewDataSource,UIActionSheetDelegate,UINavigationControllerDelegate,UIImagePickerControllerDelegate,UIAlertViewDelegate>
{
    UITableView * _tableView;
    UIImagePickerController * _pickerVC;
    NSMutableArray * dataSource;
    
    NSString * code;
    NSString * yqcode;
    
    NSString * _timeStr;
    
    NSString * _date;
}
@property (strong, nonatomic) CCEaseRefresh *refreshView;


@end

static int page = 1;

@implementation InServerCompangyVC
#pragma mark - 视图的生命周期
-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:YES];

  
}
-(void)viewDidLoad
{
    [super viewDidLoad];
    
//    UIBarButtonItem *backBarButton = [[UIBarButtonItem alloc] initWithTitle:@"" style:UIBarButtonItemStyleDone target:nil action:nil];
//    
//    self.navigationItem.backBarButtonItem = backBarButton;
    
    //[self appearAlertView];
    
    [self main];
    [self addRightItems];
    
}
-(void)main
{
    dataSource = [[NSMutableArray alloc]init];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(oneBtnSected:) name:@"oneBtnSected" object:nil];

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(caledarNoti:) name:@"caledarNoti" object:nil];

    [self.view setBackgroundColor:[UIColor whiteColor]];
    _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT) style:UITableViewStyleGrouped];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.tableHeaderView = [self createTableViewHeaderImg];
    [_tableView registerNib:[UINib nibWithNibName:@"InServeiCompanyFirCell" bundle:nil] forCellReuseIdentifier:@"InServeiCompanyFirCell"];
    [self.view addSubview:_tableView];
    
    _tableView.tableFooterView = [UIView new];
    _tableView.contentInset = UIEdgeInsetsMake(64, 0, 0, 0);
   

    self.automaticallyAdjustsScrollViewInsets = NO;
    
    if ([_isMydyna isEqualToString:@"mine"]) {
        if (_code) {
            code = _code;
        }
        else
        {
            code = [Singleton share].code;
        }
        NSLog(@"*********%@",code);
        yqcode = @"0";
        self.navigationItem.title = @"个人动态";



        
    }
    else
    {
        code = [Singleton share].code;
        yqcode = [Singleton share].yqcode;
        
        self.navigationItem.title = @"企业动态";
        
        //下拉刷新
        __weak typeof(self)weakSelf = self;
        
        
        
        
        
        //上拉加载
        _tableView.footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
            [weakSelf requestDataMydynaWithdate:_date andPage:++page];
        }];

        
    }
   // [self requestDataMydynaWithdate:@"" andPage:page];
    
    //下拉刷新
    __weak typeof(self)weakSelf = self;
    

    
    
    self.refreshView = [[CCEaseRefresh alloc] initInScrollView:_tableView];
    [self.refreshView addTarget:self action:@selector(ViewDidBeginRefreshing:) forControlEvents:UIControlEventValueChanged];
    
    [self.refreshView beginRefreshing];
    
    //上拉加载
    _tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
        __strong typeof(weakSelf)strongSelf = weakSelf;
        [strongSelf requestDataMydynaWithdate:_date andPage:++page];
    }];

    
}

#pragma mark--- 下拉刷新点击
- (void)ViewDidBeginRefreshing:(CCEaseRefresh *)refreshControl {
   
    page = 1;

    double delayInSeconds = 1.0;
    dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC);
    dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
        
        if (_timeStr) {
            [self requestDataMydynaWithdate:_timeStr andPage:page];
            
        }else{
        
            [self requestDataMydynaWithdate:@"" andPage:page];
        }
        
      
        
        
        
        
        
        
    });
    
    
    
}
-(void)requestDataMydynaWithdate:(NSString *)date andPage:(int)iPage
{
    
    [self reachability];

    _date = date;
    __weak typeof(self)weakSelf = self;

    [U76HONet zhichangDynationWithaction:@"" code:code qycode:yqcode page:[NSString stringWithFormat:@"%i",iPage] pnum:@"10" date:date andDataBlock:^(DynaModel *model) {
        
        
       
        
        NSLog(@"获取动态成功了");
        
        if (model.logo) {
            UIImageView * logoImgV = (UIImageView *)[self.view viewWithTag:712];
            [logoImgV sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",U76HOURL,model.logo]] placeholderImage:[UIImage imageNamed:@""]];
        }
        
        
        if ([_isMydyna isEqualToString:@"mine"]) {
            
        }
        else
        {
            
            //企业自检跳出框
            if (model.istest == YES) {
                [weakSelf appearAlertView];
            }
        }
        NSString * pageString = [NSString stringWithFormat:@"%i",iPage];
        
       
        
        if ([pageString isEqualToString:@"1"]) {
            if (dataSource.count!=0) {
                [dataSource removeAllObjects];
            }
            [dataSource addObjectsFromArray:model.dynaArr];
            [_tableView reloadData];
            [self.refreshView endRefreshing];
            [_tableView.mj_footer resetNoMoreData];
        }
        else if ([_tableView.mj_footer isRefreshing])
        {
            if (model.dynaArr.count) {
                [dataSource addObjectsFromArray:model.dynaArr];
                [_tableView reloadData];
                [_tableView.mj_footer endRefreshing];
            }
            else
            {
                [_tableView.mj_footer endRefreshingWithNoMoreData];
                return ;
            }
        }
        else
        {
            if (dataSource.count!=0) {
                [dataSource removeAllObjects];
            }
            [dataSource addObjectsFromArray:model.dynaArr];
            [_tableView reloadData];
        }
        
        
        
        
    }];
}

- (void)reachability
{
    // 检测网络连接状态
    [[AFNetworkReachabilityManager sharedManager] startMonitoring];
    // 连接状态回调处理
    /* AFNetworking的Block内使用self须改为weakSelf, 避免循环强引用, 无法释放 */
    //    __weak typeof(self) weakSelf = self;
    [[AFNetworkReachabilityManager sharedManager] setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status)
     {
         switch (status)
         {
             case AFNetworkReachabilityStatusUnknown:
                 // 回调处理
                 [self.refreshView endRefreshing];
                 break;
             case AFNetworkReachabilityStatusNotReachable:
                 //                 [Singleton alerSendMessage:@""];
                 [self.refreshView endRefreshing];                 // 回调处理
                 break;
             case AFNetworkReachabilityStatusReachableViaWWAN:
                 // 回调处理
                 break;
             case AFNetworkReachabilityStatusReachableViaWiFi:
                 // 回调处理
                 break;
             default:
                 break;
         }
     }];
}
-(void)zanortuWithType:(NSString *)type andInfoid:(NSString *)infoid andTag:(int)tag andBaseinfomodel:(DynaBaseinfoModel *)model
{
    if ([self isLogin]) {
        __weak typeof(self)weakSelf = self;
        [U76HONet tuZanWitnAction:@"" code:[Singleton share].code Msgid:infoid Opinion:type andDataBlock:^(NSDictionary *dic) {
            
            UIButton * btn = (UIButton *)[weakSelf.view viewWithTag:tag];
            NSLog(@"**********************%i",tag);
            NSString * str = btn.titleLabel.text;
            NSArray * arr = [str componentsSeparatedByString:@" "];
            NSString * tempStr = arr.lastObject;
            int i = [tempStr intValue];
            [btn setTitle:[NSString stringWithFormat:@" %i",i+1] forState:UIControlStateNormal];
            
            
            if ([type isEqualToString:@"1"]) {
                model.praise = btn.titleLabel.text;
            }
            else
            {
                model.vomit = btn.titleLabel.text;
            }
            
            
            NSLog(@"图赞成功列");
            
        }];
    }
    
}

#pragma mark - _tableViewDelegate
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return dataSource.count;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    DynaOneModel * onemodel = dataSource[section];
    NSArray * array = onemodel.dynaCommendmodelArr;
    if (array.count != 0) {
        if (array.count == 1) {
            return 1;
        }
        else
        {
            return 2;
        }
    }
    return 0;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    InServeiCompanyFirCell * cell = [tableView dequeueReusableCellWithIdentifier:@"InServeiCompanyFirCell" forIndexPath:indexPath];
    DynaOneModel * onemodel = dataSource[indexPath.section];
    NSArray * array = onemodel.dynaCommendmodelArr;
    cell.dynaCommendInfomodel = array[indexPath.row];
    return cell;
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    return  [self createSectionViewwithSection:(int)section];
}
-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    return [self createSectionFooterViewwithsection:(int)section];
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    DynaOneModel * onemodel = dataSource[section];
    DynaBaseinfoModel * baseInfomodel = onemodel.baseInfomodel;
    //动态内容
    NSString * contentStr ;
//    NSAttributedString * Str = [[NSAttributedString alloc] initWithData:[baseInfomodel.content dataUsingEncoding:NSUnicodeStringEncoding] options:@{ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType } documentAttributes:nil error:nil];
    contentStr = baseInfomodel.content;
    CGFloat height = [contentStr heightOfSize:17 width:SCREEN_WIDTH-30];
    
    int j = 0;
    //动态中发表的图片
    CGFloat tempHeight;
    
    if (![baseInfomodel.pic isEqualToString:@""]) {
        CGFloat imgVwidth = (SCREEN_WIDTH - SIZETO6W(15)*2 - SIZETO6W(10)*2) / 3;
        
        NSArray * imgUrlArr = [baseInfomodel.pic componentsSeparatedByString:@"|"];
        
        for (int i = 0; i<imgUrlArr.count; i++) {
            
            int row=i/3;//行号
            j = row;
        }
        tempHeight = imgVwidth + j* (imgVwidth+SIZETO6W(10));
    }
    else
    {
        tempHeight = 0;
    }

    if ([baseInfomodel.position isEqualToString:@""] || baseInfomodel.position == nil) {
        
    }
    else
    {
        tempHeight = tempHeight + 12 + SIZETO6H(10);
    }


    if (onemodel.dynaZanmodelArr.count != 0) {
        NSArray * arr3 = onemodel.dynaZanmodelArr;
        
//        if (arr3.count!=0) {
//            tempHeight += 40;
//        }
        
        if (arr3.count!=0) {
            CGFloat zanHeight = 0;
            NSArray * zanArr = arr3;
            NSString * str = @"快来赞吧";
            if (zanArr.count == 1) {
                DynaZanModel * zanmodel = [zanArr objectAtIndex:0];
                NSString * str1 = zanmodel.username;
                str = [NSString stringWithFormat:@"%@等1人",str1];
            }
            else if(zanArr.count == 2)
            {
                DynaZanModel * zanmodel2 = [zanArr objectAtIndex:0];
                NSString * str2 = zanmodel2.username;
                
                DynaZanModel * zanmodel3 = [zanArr objectAtIndex:1];
                NSString * str3 = zanmodel3.username;
                
                str = [NSString stringWithFormat:@"%@，%@等%i人",str2,str3,2];
            }
            else
            {
                DynaZanModel * zanmodel2 = [zanArr objectAtIndex:0];
                NSString * str2 = zanmodel2.username;
                
                DynaZanModel * zanmodel3 = [zanArr objectAtIndex:1];
                NSString * str3 = zanmodel3.username;
                
                DynaZanModel * zanmodel4 = [zanArr objectAtIndex:2];
                NSString * str4 = zanmodel4.username;
                
                str = [NSString stringWithFormat:@"%@,%@,%@等%i人",str2,str3,str4,(int)[zanArr count]];
                
            }
            zanHeight = [str heightOfSize:20 width:SCREEN_WIDTH - 30] + 5;
            tempHeight += zanHeight;
        }

    }
    
    return 130+tempHeight + height;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    DynaOneModel * onemodel = dataSource[indexPath.section];
    NSArray * array = onemodel.dynaCommendmodelArr;
    if (array.count == 1) {
        DynaCommendModel * model = array[indexPath.row];
        CGFloat tempHeight = [model.content heightOfSize:17 width:(SCREEN_WIDTH - 53 - SIZETO6W(15))];
        return tempHeight + 30+10;
    }
    else
    {
        DynaCommendModel * model = array[indexPath.row];
        CGFloat tempHeight = [model.content heightOfSize:17 width:(SCREEN_WIDTH - 53 - SIZETO6W(15))];
        return tempHeight + 30+10;
    }
    
    return 0;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 46;
}
#pragma mark - createSectionView
-(UIView *)createSectionViewwithSection:(int)section
{
//    AllCommendBaseInfoModel * baseInfomodel = myModel.baseInfomodel;
    
    DynaOneModel * oneModel = dataSource[section];
    
    if (oneModel.baseInfomodel!=nil) {
        DynaBaseinfoModel * baseInfomodel = oneModel.baseInfomodel;
        UIView * sectionView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 170+(SCREEN_WIDTH-30)/3)];
        sectionView.backgroundColor = [UIColor whiteColor];
        sectionView.userInteractionEnabled = YES;
        
        //头像
        UIImageView * headerImgV = [[UIImageView alloc]initWithFrame:CGRectMake(SIZETO6W(15), 15, 40, 40)];
        headerImgV.layer.masksToBounds = YES;
        headerImgV.layer.cornerRadius = 20;
        [headerImgV sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",U76HOURL,baseInfomodel.headpic]] placeholderImage:[UIImage imageNamed:@"公司简介成长－默认头像"]];
        [sectionView addSubview:headerImgV];
        //用户名
        UILabel * nameLab = [[UILabel alloc]initWithFrame:CGRectMake(headerImgV.right+SIZETO6W(10), 15, 100, 20)];
        nameLab.text = baseInfomodel.username;
        nameLab.font = [UIFont systemFontOfSize:15];
        nameLab.textColor = UIColorFromRGB(0x333944);
        [sectionView addSubview:nameLab];
        
        UILabel * dateLab = [[UILabel alloc]initWithFrame:CGRectMake(headerImgV.right+SIZETO6W(10), nameLab.bottom, 200, 20)];
        //时间
        NSDateFormatter * formatter = [[NSDateFormatter alloc]init];
        formatter.dateFormat = @"yyyy-MM-dd HH:mm:ss";
        NSString * dataCh = baseInfomodel.addtime;
        NSString * dataStr = [dataCh substringToIndex:dataCh.length];
        NSDate * data = [[NSDate dateWithTimeIntervalSince1970:[dataStr integerValue]] dateByAddingTimeInterval:8*3600];
        NSString * lastStr = [NSString stringWithFormat:@"%@",data];
        NSArray * lastArr = [lastStr componentsSeparatedByString:@" "];
        NSMutableArray * mArr = [[NSMutableArray alloc]init];
        for (int i = 0; i<2; i++) {
            [mArr addObject:lastArr[i]];
        }
        dateLab.text = [mArr componentsJoinedByString:@" "];
        
        dateLab.font = [UIFont systemFontOfSize:15];
        dateLab.textColor = UIColorFromRGB(0x94999F);
        [sectionView addSubview:dateLab];
        
        //动态内容
        NSString * contentStr ;
        
//        NSAttributedString * Str = [[NSAttributedString alloc] initWithData:[baseInfomodel.content dataUsingEncoding:NSUnicodeStringEncoding] options:@{ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType } documentAttributes:nil error:nil];
//        contentStr = [Str string];
        contentStr = baseInfomodel.content;
        CGFloat height = [contentStr heightOfSize:17 width:SCREEN_WIDTH-30];
        
        UILabel * contentLab = [[UILabel alloc]initWithFrame:CGRectMake(SIZETO6W(15), dateLab.bottom+SIZETO6H(15), SCREEN_WIDTH-30, height)];
        contentLab.numberOfLines = 0;
        contentLab.textColor = UIColorFromRGB(0x333944);
        contentLab.text = contentStr;
        [sectionView addSubview:contentLab];
        
        //动态中发表的图片
        CGFloat imgVwidth = (SCREEN_WIDTH - SIZETO6W(15)*2 - SIZETO6W(10)*2) / 3;
        
        int j = 0;
        CGFloat tempHeight;
        
        
        if (![baseInfomodel.pic isEqualToString:@""]) {
            NSRange range = [baseInfomodel.pic rangeOfString:@"1"];
            if (range.location != NSNotFound) {
                NSArray * imgUrlArr = [baseInfomodel.pic componentsSeparatedByString:@"|"];
                
                
                for (int i = 0; i<imgUrlArr.count; i++) {
                    
                    int row=i/3;//行号
                    int loc=i%3;//列号
                    j = row;
                    UIImageView * imgV = [[UIImageView alloc]initWithFrame:CGRectMake(SIZETO6W(15) + loc * (imgVwidth+SIZETO6W(10)), contentLab.bottom + row*(SIZETO6W(10) + imgVwidth), imgVwidth, imgVwidth)];
                    imgV.userInteractionEnabled = YES;
                    NSLog(@"%@",[NSString stringWithFormat:@"%i%i",i+1,section]);
                    imgV.tag = [[NSString stringWithFormat:@"%i%i",i+1,section] intValue];
//                    [imgV setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",U76HOURL,imgUrlArr[i]]] placeholderImage:[UIImage imageNamed:@"在职公司－默认动态图"] ];
                    
                    [imgV sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",U76HOURL,imgUrlArr[i]]] placeholderImage:[UIImage imageNamed:@"在职公司－默认动态图"] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
                        
                        
                        
                        
                        dispatch_async(dispatch_get_main_queue(), ^{
                            float tempW = (image.size.height - image.size.width) >= 0 ? image.size.width : image.size.height;
                            imgV.image = [image croppedImageAtFrame:CGRectMake((image.size.height - image.size.width) >= 0 ? 0 : (image.size.width - image.size.height)/2, (image.size.width - image.size.height) >= 0 ? 0 : (image.size.height - image.size.width)/2 , tempW, tempW)];
                        });
                        

                    }];

                    
                    //点击手势
                    UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapAction:)];
                    tap.numberOfTapsRequired = 1;
                    tap.numberOfTouchesRequired = 1;

                    [imgV addGestureRecognizer:tap];
                    [sectionView addSubview:imgV];
                }
            }
            else
            {
                UIImageView * imgV = [[UIImageView alloc]initWithFrame:CGRectMake(SIZETO6W(15) , contentLab.bottom, imgVwidth, imgVwidth)];
                imgV.userInteractionEnabled = YES;
//                [imgV setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",U76HOURL,baseInfomodel.pic]] placeholderImage:[UIImage imageNamed:@"在职公司－默认动态图"]];
                
                    [imgV sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",U76HOURL,baseInfomodel.pic]] placeholderImage:[UIImage imageNamed:@"在职公司－默认动态图"] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
                        
                        
                        
                        dispatch_async(dispatch_get_main_queue(), ^{
                            float tempW = (image.size.height - image.size.width) >= 0 ? image.size.width : image.size.height;
                            imgV.image = [image croppedImageAtFrame:CGRectMake((image.size.height - image.size.width) >= 0 ? 0 : (image.size.width - image.size.height)/2, (image.size.width - image.size.height) >= 0 ? 0 : (image.size.height - image.size.width)/2 , tempW, tempW)];
                        });

                }];
                
                //点击手势
                UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapAction:)];
                tap.numberOfTapsRequired = 1;
                tap.numberOfTouchesRequired = 1;
                imgV.tag = [[NSString stringWithFormat:@"%i%i",1,section] intValue];
                [imgV addGestureRecognizer:tap];
                
                [sectionView addSubview:imgV];
                
            }
            
            tempHeight = imgVwidth + j* (imgVwidth+SIZETO6W(10));


        }
        else
        {
            tempHeight = 0;
        }
        
        if ([baseInfomodel.position isEqualToString:@""] || baseInfomodel.position == nil) {
            
        }
        else
        {
            UIImageView * positionImgV = [[UIImageView alloc]initWithFrame:CGRectMake(SIZETO6W(15), contentLab.bottom+SIZETO6H(10) + SIZETO6W(5) + tempHeight, 12, 12)];
            positionImgV.image = [UIImage imageNamed:@"位置"];
            UILabel * lab = [[UILabel alloc]initWithFrame:CGRectMake(positionImgV.right + 5, positionImgV.top, SCREEN_WIDTH - positionImgV.right - 5 - SIZETO6W(15), 12)];
            lab.text = baseInfomodel.position;
            lab.textColor = UIColorFromRGB(0x94999f);
            [sectionView addSubview:lab];
            lab.font = [UIFont systemFontOfSize:12];
            [sectionView addSubview:positionImgV];
            tempHeight = tempHeight + 12 + SIZETO6H(10);
        }
        
        UIButton * zanBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        zanBtn.frame = CGRectMake(SIZETO6W(15), contentLab.bottom+SIZETO6H(10) + SIZETO6W(5) + tempHeight, 60, 30);
        [zanBtn setImage:[UIImage imageNamed:@"点赞"] forState:UIControlStateNormal];
        [zanBtn setTitle:baseInfomodel.praise forState:UIControlStateNormal];
        [zanBtn setTitleColor:UIColorFromRGB(0x333944) forState:UIControlStateNormal];
        [zanBtn setBackgroundColor:UIColorFromRGB(0xEDF0EF)];
        zanBtn.layer.masksToBounds = YES;
        zanBtn.layer.cornerRadius = 5;
        zanBtn.tag = 90000+section;
        [zanBtn addTarget:self action:@selector(zanBtnAction:) forControlEvents:UIControlEventTouchUpInside];
        [sectionView addSubview:zanBtn];
          
        UIButton * tuBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        tuBtn.frame = CGRectMake(zanBtn.right+SIZETO5W(10), zanBtn.top, 60, 30);
        [tuBtn setBackgroundColor:UIColorFromRGB(0xEDF0EF)];
        [tuBtn setImage:[UIImage imageNamed:@"吐"] forState:UIControlStateNormal];
        tuBtn.tag = 9000000+section;
        [tuBtn setTitle:[NSString stringWithFormat:@" %@",baseInfomodel.vomit] forState:UIControlStateNormal];
        [tuBtn addTarget:self action:@selector(tuAction:) forControlEvents:UIControlEventTouchUpInside];
        [tuBtn setTitleColor:UIColorFromRGB(0x333944) forState:UIControlStateNormal];
        tuBtn.layer.masksToBounds = YES;
        tuBtn.layer.cornerRadius = 5;
        [sectionView addSubview:tuBtn];
        
        
        
        UIButton * pinglunBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        pinglunBtn.frame = CGRectMake(tuBtn.right+SIZETO5W(10), zanBtn.top, 60, 30);
        [pinglunBtn setBackgroundColor:UIColorFromRGB(0xEDF0EF)];
        [pinglunBtn setImage:[UIImage imageNamed:@"评论"] forState:UIControlStateNormal];
        [pinglunBtn setTitle:[NSString stringWithFormat:@" %@",baseInfomodel.discuss] forState:UIControlStateNormal];
        [pinglunBtn addTarget:self action:@selector(pinglunAction:) forControlEvents:UIControlEventTouchUpInside];
        [pinglunBtn setTitleColor:UIColorFromRGB(0x333944) forState:UIControlStateNormal];
        pinglunBtn.layer.masksToBounds = YES;
        pinglunBtn.layer.cornerRadius = 5;
        pinglunBtn.tag = section + 990000000;
        [sectionView addSubview:pinglunBtn];
        CGFloat zanHeight = 0;
        if (oneModel.dynaZanmodelArr.count != 0) {
            NSArray * arr3 = oneModel.dynaZanmodelArr;
            
            if (arr3.count!=0) {
                NSArray * zanArr = arr3;
                UIImageView * imgV = [[UIImageView alloc]initWithFrame:CGRectMake(zanBtn.left, zanBtn.bottom + 15, 20, 20)];
                imgV.image = [UIImage imageNamed:@"点赞"];
                [sectionView addSubview:imgV];
                UILabel * zanPeopleLab = [[UILabel alloc]initWithFrame:CGRectMake(imgV.right+5, imgV.top+5, 250, 20)];
                NSString * str = @"快来赞吧";
                if (zanArr.count == 1) {
                    DynaZanModel * zanmodel = [zanArr objectAtIndex:0];
                    NSString * str1 = zanmodel.username;
                    str = [NSString stringWithFormat:@"%@ 等1人",str1];
                }
                else if(zanArr.count == 2)
                {
                    DynaZanModel * zanmodel2 = [zanArr objectAtIndex:0];
                    NSString * str2 = zanmodel2.username;
                    
                    DynaZanModel * zanmodel3 = [zanArr objectAtIndex:1];
                    NSString * str3 = zanmodel3.username;
                    
                    str = [NSString stringWithFormat:@"%@,%@ 等%i人",str2,str3,2];
                }
                else
                {
                    DynaZanModel * zanmodel2 = [zanArr objectAtIndex:0];
                    NSString * str2 = zanmodel2.username;
                    
                    DynaZanModel * zanmodel3 = [zanArr objectAtIndex:1];
                    NSString * str3 = zanmodel3.username;
                    
                    DynaZanModel * zanmodel4 = [zanArr objectAtIndex:2];
                    NSString * str4 = zanmodel4.username;
                    
                    str = [NSString stringWithFormat:@"%@,%@,%@ 等%i人",str2,str3,str4,(int)[zanArr count]];
                    
                }
                zanPeopleLab.text = str;
                zanHeight = [str heightOfSize:20 width:SCREEN_WIDTH - 30] + 5;
                zanPeopleLab.frame = CGRectMake(imgV.right+5, imgV.top, 250,zanHeight);
                [sectionView addSubview:zanPeopleLab];
                
            }
            
        }
        
        sectionView.frame = CGRectMake(0, 0, SCREEN_WIDTH, 130+tempHeight+height - 20 + zanHeight);
        return sectionView;

    }
    return nil;
}
-(void)tapAction:(UITapGestureRecognizer *)tap
{
    NSLog(@"tapAction is did");
    int tag = (int)tap.view.tag;
    NSLog(@"%i",tag);
    NSString * tagStr = [NSString stringWithFormat:@"%i",tag];
    int i = [[tagStr substringToIndex:1] intValue];
    int section = [[tagStr substringFromIndex:1] intValue];
    DynaOneModel * oneModel = dataSource[section];
    
    if (oneModel.baseInfomodel!=nil) {
        if (![oneModel.baseInfomodel.pic isEqualToString:@""]) {
            NSArray * imgUrlArr = [oneModel.baseInfomodel.pic componentsSeparatedByString:@"|"];
            BoostViewController * boostVC = [[BoostViewController alloc]init];
            boostVC.imgArr = imgUrlArr;
            boostVC.contentStr = oneModel.baseInfomodel.content;
            boostVC.i = i;
            [self.navigationController pushViewController:boostVC animated:YES];
        }
    }

}
-(UIView *)createSectionFooterViewwithsection:(int)section
{
    UIView * view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 46)];
    view.backgroundColor = [UIColor whiteColor];
    
    UIButton * btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.frame = CGRectMake(SIZETO6W(20), 0, 80, 30);
    btn.tag = section + 90000000;
    [btn setTitle:@"全部评论" forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(allCommentAction:) forControlEvents:UIControlEventTouchUpInside];
    [btn setTitleColor:UIColorFromRGB(0x5CC02E) forState:UIControlStateNormal];
    [view addSubview:btn];
    
    UIView * lineVbottom = [[UIView alloc]initWithFrame:CGRectMake(0, 45, SCREEN_WIDTH, 0.5)];
    lineVbottom.backgroundColor = [UIColor lightGrayColor];
    [view addSubview:lineVbottom];
    
    return view;
}

-(UIView *)createTableViewHeaderImg
{
    UIView * header = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SIZETO6H(160))];
    UIImageView * img = [[UIImageView alloc]initWithFrame:header.frame];
    img.image = [UIImage imageNamed:@"顶部默认背景图"];
    [header addSubview:img];
    
    UIImageView * headerimgV = [[UIImageView alloc]initWithFrame:CGRectMake(SCREEN_WIDTH/2-SIZETO6W(80)/2, img.height/2-SIZETO6W(80)/2, SIZETO6W(80), SIZETO6W(80))];
    headerimgV.tag = 712;
    headerimgV.image = [UIImage imageNamed:@"顶部默认头像"];
    headerimgV.layer.masksToBounds = YES;
    headerimgV.layer.cornerRadius = headerimgV.width/2;
    
    
    [header addSubview:headerimgV];
    
    
    return header;
}
#pragma mark - 按钮的点击事件们
-(void)zanBtnAction:(UIButton *)btn
{
    int tag = (int)btn.tag - 90000;
    DynaOneModel * onemodel = dataSource[tag];
    DynaBaseinfoModel * model = onemodel.baseInfomodel;
    
    
//    NSString * str = btn.titleLabel.text;
//    NSArray * arr = [str componentsSeparatedByString:@" "];
//    NSString * tempStr = arr.lastObject;
//    int i = [tempStr intValue];
//    [btn setTitle:[NSString stringWithFormat:@" %i",i+1] forState:UIControlStateNormal];
    
    //调用图赞接口
    [self zanortuWithType:@"1" andInfoid:model.ids andTag:(int)btn.tag andBaseinfomodel:model];
}
-(void)tuAction:(UIButton *)btn
{
    int tag = (int)btn.tag - 9000000;
    DynaOneModel * onemodel = dataSource[tag];
    DynaBaseinfoModel * model = onemodel.baseInfomodel;
    
    
//    NSString * str = btn.titleLabel.text;
//    NSArray * arr = [str componentsSeparatedByString:@" "];
//    NSString * tempStr = arr.lastObject;
//    int i = [tempStr intValue];
//    [btn setTitle:[NSString stringWithFormat:@" %i",i+1] forState:UIControlStateNormal];
    
    //调用图赞接口
    [self zanortuWithType:@"2" andInfoid:model.ids andTag:(int)btn.tag andBaseinfomodel:model];
}
-(void)photoAction:(id)sender
{
    UIActionSheet * sheet = [[UIActionSheet alloc]initWithTitle:nil delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@"文字",@"拍照",@"从手机相册选择", nil];
    [sheet showInView:self.view];
}
-(void)allCommentAction:(UIButton *)sender
{
    int tag = (int)sender.tag - 90000000;
    DynaOneModel * onemodel = dataSource[tag];
    DynaBaseinfoModel * model = onemodel.baseInfomodel;
    AllCompentViewController * allCommentVC = [[AllCompentViewController alloc]init];
//    allCommentVC.delegate = self;
//    __weak typeof(self)weakSelf = self;
    allCommentVC.dynaOneModel = onemodel;
    [allCommentVC setRefreshBlock:^(DynaOneModel * Model) {
        [dataSource replaceObjectAtIndex:tag withObject:Model];
        [_tableView reloadData];
    }];
//    [allCommentVC setRefreshBlock:^{
//        [weakSelf requestDataMydynaWithdate:_date];
//    }];
    allCommentVC.infoId = model.ids;
    [self.navigationController pushViewController:allCommentVC animated:YES];
}

-(void)pinglunAction:(UIButton *)sender
{
    int tag = (int)sender.tag - 990000000;
    DynaOneModel * onemodel = dataSource[tag];
    DynaBaseinfoModel * model = onemodel.baseInfomodel;
    AllCompentViewController * allCommentVC = [[AllCompentViewController alloc]init];
//    __weak typeof(self)weakSelf = self;
//    [allCommentVC setRefreshBlock:^{
//        [weakSelf requestDataMydynaWithdate:_date];
//    }];
    allCommentVC.dynaOneModel = onemodel;
    [allCommentVC setRefreshBlock:^(DynaOneModel * Model) {
        [dataSource replaceObjectAtIndex:tag withObject:Model];
        [_tableView reloadData];
    }];

    allCommentVC.infoId = model.ids;
    [self.navigationController pushViewController:allCommentVC animated:YES];
}
#pragma mark - 消息通知
-(void)caledarNoti:(NSNotification *)noti
{
    NSLog(@"在益企的通知接收到的消息%@",noti.object);
    NSArray * arr = noti.object;
    NSMutableArray * mArr = [[NSMutableArray alloc]init];
    [mArr addObject:arr.firstObject];
    if ([[[arr objectAtIndex:1] stringValue] length]  == 1) {
        NSString *str = [NSString stringWithFormat:@"0%@",[arr objectAtIndex:1]];
        [mArr addObject:str];
    }
    else
    {
        [mArr addObject:[arr objectAtIndex:1]];
    }
    
    if ([[arr.lastObject stringValue] length] == 1) {
        NSString *str = [NSString stringWithFormat:@"0%@",[arr objectAtIndex:2]];
        [mArr addObject:str];
    }
    else
    {
        [mArr addObject:[arr objectAtIndex:2]];
    }
    
    NSString * timeStr = [mArr componentsJoinedByString:@""];
    NSLog(@"timeStr is %@",timeStr);
    
    _timeStr = timeStr;
    
    
    page = 1;
    [self requestDataMydynaWithdate:timeStr andPage:page];
}
//将选中的图片付给头像
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    UIImage * selectImg = [info objectForKey:UIImagePickerControllerOriginalImage];
    
    [self dismissViewControllerAnimated:YES completion:nil];

    SendPictureViewController * sendpicVC = [[SendPictureViewController alloc]init];
    __weak typeof(self)weakSelf = self;
    sendpicVC.isNeedRefresh = YES;
    [sendpicVC setRefreshBlock:^{
        page = 1;
        [weakSelf requestDataMydynaWithdate:_date andPage:page];
    }];
    sendpicVC.img = selectImg;
    [self.navigationController pushViewController:sendpicVC animated:YES];
    
//    headerImg = selectImg;
//    
//    
//    [_tabelView reloadData];
}

#pragma mark - addRightItems
-(void)addRightItems
{
    UIButton * photoBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    photoBtn.frame = CGRectMake(0, 0, 30, 30);
    [photoBtn setImage:[UIImage imageNamed:@"相机"] forState:UIControlStateNormal];
    [photoBtn addTarget:self action:@selector(photoAction:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem * oneItem = [[UIBarButtonItem alloc]initWithCustomView:photoBtn];
    
    UIButton * biaoBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    biaoBtn.frame = CGRectMake(0, 0, 30, 30);
    [biaoBtn setImage:[UIImage imageNamed:@"时间"] forState:UIControlStateNormal];
    [biaoBtn addTarget:self action:@selector(biaoAction:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem * twoItem = [[UIBarButtonItem alloc]initWithCustomView:biaoBtn];
    
    self.navigationItem.rightBarButtonItems = @[oneItem,twoItem];
}

#pragma mark - ActionSheetDelegate
-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex == 0) {
        SendPictureViewController * sendPictureVC = [[SendPictureViewController alloc]init];
        __weak typeof(self)weakSelf = self;
        sendPictureVC.isNeedRefresh = YES;
        [sendPictureVC setRefreshBlock:^{
            page = 1;
            [weakSelf requestDataMydynaWithdate:_date andPage:page];
        }];
        [self.navigationController pushViewController:sendPictureVC animated:YES];
    }
    else if (buttonIndex == 1)
    {
        _pickerVC = [[UIImagePickerController alloc]init];
        [_pickerVC setSourceType:UIImagePickerControllerSourceTypeCamera];
        _pickerVC.delegate= self;
        [self presentViewController:_pickerVC animated:YES completion:nil];
    }
    else if (buttonIndex == 2)
    {
        SendPictureViewController * sendPictureVC = [[SendPictureViewController alloc]init];
        __weak typeof(self)weakSelf = self;
        sendPictureVC.isNeedRefresh = YES;
        [sendPictureVC setRefreshBlock:^{
            page = 1;
            [weakSelf requestDataMydynaWithdate:_date andPage:page];
        }];
        sendPictureVC.sendPicture = YES;
        [self.navigationController pushViewController:sendPictureVC animated:YES];
    }
}
#pragma mark - 弹出企业内部自检
-(void)appearAlertView
{
    [Singleton alerOneBtnMessage:@"企业自检" content:@"企业邀请您参加内部自检" btnName:@"进入"];
}
-(void)oneBtnSected:(NSNotification *)noti
{
    NSLog(@"oneBtn is selected");
    if ([self isLogin]) {
        CompanySelfCheckVC * compangSelfCheckVC = [[CompanySelfCheckVC alloc]init];
        [self.navigationController pushViewController:compangSelfCheckVC animated:YES];
    }
    
}


-(BOOL)isLogin
{
    if ([Singleton share].code) {
        NSLog(@"登录了");
        return YES;
        
    }
    else
    {
        //        NSLog(@"还没登录呢");
        //        LoginViewController * loginVC = [[LoginViewController alloc]init];
        //        [self.navigationController pushViewController:loginVC animated:NO];
        //        return NO;
        if (SYSTEM_VERSION >= 8.0) {
            UIAlertController * alerVC = [UIAlertController alertControllerWithTitle:@"提示" message:@"您还没有登录是否登录" preferredStyle:UIAlertControllerStyleAlert];
            [alerVC addAction:[UIAlertAction actionWithTitle:@"登录" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
                
                LoginViewController * loginVC = [[LoginViewController alloc]init];
                UINavigationController *nvc = [[UINavigationController alloc]initWithRootViewController:loginVC];
                nvc.navigationBarHidden = YES;
                
                [self presentViewController:nvc animated:YES completion:nil];
                
            }]];
            [alerVC addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
                
            }]];
            [self.view.window.rootViewController presentViewController:alerVC animated:YES completion:nil];
            
        }
        else
        {
            UIAlertView * alert = [[UIAlertView alloc]initWithTitle:@"提示" message:@"您还没有登录是否登录" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
            [alert show];
        }
        return NO;
    }
}
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    int i = (int)buttonIndex;
    if (i == 0) {
        
    }
    else if(i == 1)
    {

        LoginViewController * loginVC = [[LoginViewController alloc]init];
        UINavigationController *nvc = [[UINavigationController alloc]initWithRootViewController:loginVC];
        nvc.navigationBarHidden = YES;
        
        [self presentViewController:nvc animated:YES completion:nil];
    }
}

-(void)biaoAction:(UIButton *)btn
{
    [Singleton alertCalendar];
    
}


-(void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
    NSLog(@"销毁了*************************");
}
@end
